﻿using System;
using System.Reflection;
using System.Configuration;
using SobFS.IDAL;
using SobFS.Common.Database;
namespace SobFS.DALFactory
{
    /// <summary>
    /// 抽象工厂模式创建DAL。
    /// <para>web.config 需要加入配置：(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口) </para>
    /// <appSettings> 
    /// <add key="DAL" value="SobFS.MySQLDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
    /// </appSettings> 
    /// </summary>
    //public sealed class DataAccess<T>
    public sealed class DataAccess
    {
        private static readonly string ASSEMBLY_PATH = ConfigurationManager.AppSettings["DAL"];
            

        /// <summary>
        /// 创建对象或从缓存获取
        /// </summary>
        private static object CreateObject(string classNamespace)
        {
            object objType = DbCacheHelper.GetCache(classNamespace);//从缓存读取
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(ASSEMBLY_PATH).CreateInstance(classNamespace);//反射创建
                    DbCacheHelper.SetCache(classNamespace, objType);// 写入缓存
                }
                catch
                { }
            }
            return objType;
        }

        ///// <summary>
        ///// 创建数据层接口
        ///// </summary>
        //public static T Create(string className)
        //{
        //    string classNamespace = ASSEMBLY_PATH + "." + className;
        //    object objType = CreateObject(classNamespace);
        //    return (T)objType;
        //}

        

        public static SobFS.IDAL.ISysFunctionMenu CreateSysFunctionMenu()
        {
            string classNamespace = ASSEMBLY_PATH + "." + typeof(SobFS.Model.SysFunctionMenu).Name;
            object objType = CreateObject(classNamespace);
            return (SobFS.IDAL.ISysFunctionMenu)objType;
        }

        public static SobFS.IDAL.ISysGroup CreateSysGroup()
        {
            string classNamespace = ASSEMBLY_PATH + "." + typeof(SobFS.Model.SysGroup).Name;
            object objType = CreateObject(classNamespace);
            return (SobFS.IDAL.ISysGroup)objType;
        }

        public static SobFS.IDAL.ISysOrganization CreateSysOrganization()
        {
            string classNamespace = ASSEMBLY_PATH + "." + typeof(SobFS.Model.SysOrganization).Name;
            object objType = CreateObject(classNamespace);
            return (SobFS.IDAL.ISysOrganization)objType;
        }

        /// <summary>
        /// 创建syspermission数据层接口。
        /// </summary>
        public static SobFS.IDAL.ISysPermission CreateSysPermission()
        {
            string classNamespace = ASSEMBLY_PATH + "." + typeof(SobFS.Model.SysPermission).Name;
            object objType = CreateObject(classNamespace);
            return (SobFS.IDAL.ISysPermission)objType;
        }

        public static SobFS.IDAL.ISysRegion CreateSysRegion()
        {
            string classNamespace = ASSEMBLY_PATH + "." + typeof(SobFS.Model.SysRegion).Name;
            object objType = CreateObject(classNamespace);
            return (SobFS.IDAL.ISysRegion)objType;
        }

        public static SobFS.IDAL.ISysUser CreateSysUser()
        {
            string classNamespace = ASSEMBLY_PATH + "." + typeof(SobFS.Model.SysUser).Name;
            object objType = CreateObject(classNamespace);
            return (SobFS.IDAL.ISysUser)objType;
        }

        public static SobFS.IDAL.ISysUserGroup CreateSysUserGroup()
        {
            string classNamespace = ASSEMBLY_PATH + "." + typeof(SobFS.Model.SysUserGroup).Name;
            object objType = CreateObject(classNamespace);
            return (SobFS.IDAL.ISysUserGroup)objType;
        }

    }
}